package com.zs.flowable.controller;


import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.flowable.engine.*;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhshuo
 * @date 2021/07/12 11:05
 **/
@Log4j2
@RestController
@AllArgsConstructor
public class FlowableController {

    RepositoryService repositoryService;

    RuntimeService runtimeService;

    TaskService taskService;

    ManagementService managementService;

    IdentityService identityService;

    HistoryService historyService;

    /**
     * 发布一个流程
     * @author zhshuo
     * @date   2021/07/12 11:14
     * @return void
     */
    @GetMapping("deployBpmn")
    public void deployBpmn(){
        Deployment deployment = repositoryService.createDeployment().addClasspathResource("test.bpmn20.xml")
                .name("测试")
                .key("deploy_test")
                .deploy();
        log.info("流程发布结果:{}",deployment);
    }

    /**
     * 开启一个流程
     * @author zhshuo
     * @date   2021/07/12 11:23
     * @return void
     */
    @GetMapping("startProcess")
    public void startProcess(){
        List<Deployment> deployments = repositoryService.createDeploymentQuery().deploymentKey("deploy_test").list();
        log.info("查询出来的最新发布的流程数据:{}",deployments);

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("test");
        log.info("最新流程已经开启:{}",processInstance);
    }

    /**
     * 任务查询
     * @author zhshuo
     * @date   2021/07/12 11:29
     * @return void
     */
    @GetMapping("queryTask")
    public void queryTask(){
        List<Task> tasks = taskService.createTaskQuery().processDefinitionKey("test").list();
        log.info("查询出来的执行中的任务:{}",tasks);
    }

    /**
     * 任务完成
     * @author zhshuo
     * @date   2021/07/12 11:31
     * @return void
     */
    @GetMapping("completeTask/{taskId}")
    public void completeTask(@PathVariable String taskId){
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        log.info("需要完成的任务的信息:{}",task);

        String processInstanceId = task.getProcessInstanceId();
        taskService.complete(taskId);
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        log.info("任务完成之后，该实例信息:{}",processInstance);
    }

    /**
     * 历史数据查询
     * @author zhshuo
     * @date   2021/07/12 11:31
     * @return void
     */
    @GetMapping("historyTaskQuery/{processInstanceId}")
    public void historyTaskQuery(@PathVariable String processInstanceId){
        HistoricTaskInstance historicTaskInstance = historyService.createHistoricTaskInstanceQuery().processInstanceId(processInstanceId).singleResult();
        log.info("历史任务实例数据:{}",historicTaskInstance);
        List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery().processInstanceId(processInstanceId).list();
        log.info("该实例下的历史任务数据:{}",list);
    }

}
